//
// Created by dqchen6 on 2023/1/30.
//

#include "asm.h"

AsmGenerator::AsmGenerator(Symtab& s,  FILE* f): symtab(s), outfile(f) {

}

AsmGenerator::~AsmGenerator() {

}

void AsmGenerator::genAsm() {
    fputs("%include 'functions.asm'\n", outfile);
    fprintf(outfile, "SECTION .data\n");
    for (Var* item: symtab.getStrs()){
        fprintf(outfile, "%s db \"%s\",0h\n", item->getName().substr(1).c_str(), item->getStrVal().c_str());
    }
    fprintf(outfile, "SECTION .text\n");
    fprintf(outfile, "\tglobal  _start\n");
    for (string name: symtab.getExternSeq()){
        fprintf(outfile, "extern %s\n",  name.c_str());
    }
    for (string name: symtab.getFuncSeq()) {
        Func *func = symtab.getFunc(name);
        vector<Order*> orders = func->getOrders();
        for (Order* order: orders){
            order->genAsm(outfile);
        }
    }
}
